// Package main provides various examples of Fyne API capabilities.
package main

import (
	"GUI/windows"
	"log"

	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/app"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/theme"
	"fyne.io/fyne/v2/widget"
)

const preferenceCurrentTutorial = "currentTutorial"

var topWindow fyne.Window

func main() {
	// windows.GGG()
	// 创建新的App类
	a := app.NewWithID("io.fyne.demo")
	a.SetIcon(theme.FyneLogo())
	logLifecycle(a) // 监听App类的生命周期
	w := a.NewWindow("IPFS浏览器")
	topWindow = w

	// w.SetMainMenu(makeMenu(a, w)) // 设置菜单栏
	w.SetMaster()

	content := container.NewMax()
	title := widget.NewLabel("Component name")
	intro := widget.NewLabel("An introduction would probably go\nhere, as well as a")
	intro.Wrapping = fyne.TextWrapWord
	// 在本程序中，被用于右侧展示栏的切换
	setTutorial := func(t windows.Tutorial) {
		// 窗口重画并刷新
		if fyne.CurrentDevice().IsMobile() {
			child := a.NewWindow(t.Title)
			topWindow = child
			child.SetContent(t.View(topWindow))
			child.Show()
			child.SetOnClosed(func() {
				topWindow = w
			})
			return
		} else {
			title.SetText(t.Title)
			intro.SetText(t.Intro)

			content.Objects = []fyne.CanvasObject{t.View(w)}
			content.Refresh()
		}
	}

	// 新的Border类，初始化上下左右边
	tutorial := container.NewBorder( // 这里创建了上下分割的边界
		//! 不展示intro
		container.NewVBox(title, widget.NewSeparator() /*, intro*/), nil, nil, nil, content)
	if fyne.CurrentDevice().IsMobile() {
		w.SetContent(makeNav(setTutorial, false))
	} else {
		split := container.NewHSplit(makeNav(setTutorial, true), tutorial) // 左右分割，展示顺序从左到右
		split.Offset = 0.2
		w.SetContent(split)
	}
	w.Resize(fyne.NewSize(640, 460))
	w.ShowAndRun()
}

func logLifecycle(a fyne.App) {
	a.Lifecycle().SetOnStarted(func() {
		log.Println("Lifecycle: Started")
	})
	a.Lifecycle().SetOnStopped(func() {
		log.Println("Lifecycle: Stopped")
	})
	a.Lifecycle().SetOnEnteredForeground(func() {
		log.Println("Lifecycle: Entered Foreground")
	})
	a.Lifecycle().SetOnExitedForeground(func() {
		log.Println("Lifecycle: Exited Foreground")
	})
}

// 生成导航栏
func makeNav(setTutorial func(tutorial windows.Tutorial), loadPrevious bool) fyne.CanvasObject {
	a := fyne.CurrentApp()

	// fmt.Println("before tree")
	// fmt.Scanln()
	// 本程序的关键结构
	tree := &widget.Tree{ // 创建树结构，这里用string替代了TreeNodeID类
		ChildUIDs: func(uid string) []string {
			return windows.TutorialIndex[uid] // 定义了导航栏的结构
		},
		IsBranch: func(uid string) bool { // 判断是否位于分支，若位于叶子节点则返回false
			children, ok := windows.TutorialIndex[uid]

			return ok && len(children) > 0
		},
		CreateNode: func(branch bool) fyne.CanvasObject {
			return widget.NewLabel("Collection Widgets")
		},
		UpdateNode: func(uid string, branch bool, obj fyne.CanvasObject) {
			t, ok := windows.Tutorials[uid]
			if !ok {
				fyne.LogError("Missing tutorial panel: "+uid, nil)
				return
			}
			obj.(*widget.Label).SetText(t.Title)
		},
		OnSelected: func(uid string) {
			if t, ok := windows.Tutorials[uid]; ok {
				a.Preferences().SetString(preferenceCurrentTutorial, uid)
				setTutorial(t)
			}
		},
	}

	if loadPrevious { // 决定是否预先加载上传界面
		currentPref := a.Preferences().StringWithFallback(preferenceCurrentTutorial, "upload")
		tree.Select(currentPref)
	}

	return container.NewBorder(nil, nil, nil, nil, tree)
}
